import api  from './api.js'

// 全局请求路径，也就是后端的请求基准路径
// const BASE_URL = 'http://oilcard.cn/api'//测试 

// const BASE_URL = api.http  //'https://oilcardtwo.iziyan.cn/api'//线上测试

// const BASE_URL = 'http://app.cqhj1688.com/api'//正式

const request = (url = '', data = {}, type = 'GET', dataType= '') => {
	//let userIdentity = uni.getStorageSync('identity')||0
	// let token='Bearer '+ uni.getStorageSync('token')
	// let header = {authorization:token}
	let header = {}
	let identity = ''
	let postData={}
	
	let token = ''
	if(uni.getStorageSync('token')){
		token = 'Bearer ' + uni.getStorageSync('token')
	}
	Object.assign(header, {
		authorization: token
	})
	// if (userIdentity == 1) {
	// 	let token = 'Bearer ' + uni.getStorageSync('token')
	// 	Object.assign(header, {
	// 		authorization: token
	// 	})
	// 	identity = '?identity=1'
	// }
	// if (userIdentity == 2) {
	// 	let token = 'Bearer ' + uni.getStorageSync('token')
	// 	Object.assign(header, {
	// 		authorization: token
	// 	})
	// 	identity = '?identity=2'
	// }

	let content_type = {}
	// if (type == "GET") {
	// 	content_type = {
	// 		'content-type': 'application/json'
	// 	}
	// 	if(JSON.stringify(data) != "{}"){
	// 		for(var item in data){
	// 			identity+='&'+ item +'=' + data[item]
	// 		}
	// 	}
	// }
	// if (type == 'POST') {
	// 	content_type = {
	// 		'content-type': 'application/x-www-form-urlencoded'
	// 	}
	// 	postData=data
	// }
	// if(dataType=='json'){
	// 	content_type = {
	// 		'content-type': 'application/json'
	// 	}
	// }
	content_type = {
		'content-type': 'application/json'
	}
	postData=data
	Object.assign(header, content_type)


	uni.showLoading({
		title: "加载中",
		mask: true,
	});
	
	if(dataType==''){
		return new Promise((resolve, reject) => {
			uni.request({
				method: type,
				url:  url + identity,
				data: postData,
				header
			}).then((response) => {
				setTimeout(function() {
					uni.hideLoading();
				}, 200);

				let [error, res] = response;
				if (res.data.code == 200||res.data.code == 0) {
					resolve(res.data);
				} else if (res.data.code == 401) {// code --401  重登
					uni.clearStorageSync()
					setTimeout(()=>{
						uni.reLaunch({
							url: '../login/login'
						})
					},1200)
				} else {
					uni.showToast({
						icon: "none",
						title: res.data.message,
						duration: 2000
					})
				}
				
			}).catch(error => {
				setTimeout(function() {
					uni.hideLoading();
				}, 200);
				
				let [err, res] = error;
				uni.showToast({
					icon: "none",
					title: res.data.message,
					duration: 2000
				})
				reject(err)
			})
		});
	}else{
		console.log('dataType',header)
		return new Promise((resolve, reject) => {
			uni.request({
				method: type,
				url:   url + identity,
				data: postData,
				dataType: 'json',
				header
			}).then((response) => {
				setTimeout(function() {
					uni.hideLoading();
				}, 200);
		
				let [error, res] = response;
				
				if (res.data.code == 200) {
					resolve(res.data);
				} else if (res.data.code == 401) {// code --401  重登
					uni.clearStorageSync();
					setTimeout(()=>{
						uni.reLaunch({
							url: '../login/login'
						})
					},1200)
				} else {
					uni.showToast({
						icon: "none",
						title: res.message,
						duration: 2000
					})
				}
				
			}).catch(error => {
				let [err, res] = error;
				uni.showToast({
					icon: "none",
					title: error,
					duration: 2000
				})
				reject(err)
			})
		});
	}
}

export default request
